/*
 * @Description: 
 * @Author: mmzhang
 * @Date: 2021-12-10 16:52:09
 * @LastEditTime: 2022-08-16 16:26:07
 */
import Opt from '@/components/dt/info/Opt.js'
import businessCommonMgr from '@/biz/businessReport/businessCommon'
import dateOpt from '@/biz/option/date.js'
import ruleOpt from '@/biz/option/rule.js'
import tool from '@/common/tool.js'

let _this = null
let companys = []
let developer = []
let channels = []
class SearchOpt extends Opt {

  constructor(vm) {
    _this = vm
    super()
    this.build()
  }

  getCompany() {
    let opt = {
      title: '区域筛选',
      key: 'companyIds',
      placeholder: '全部',
      tree: {
        options: companys,
      }
    }
    let params = {
      isTwoLevel: true,
    }
    businessCommonMgr
      .getOrganizationTree(params)
      .then((res) => {
        companys = res
        opt.tree.options = companys
      })
    return opt
  }

  getDeveloper(value = '', flag) {
    let opt = {
      title: '开发商简称',
      key: 'developerAbbreviations',
      placeholder: '全部',
      xl: 4,
      tree: {
        options: developer,
        searchBtn: true,
        searchFun: (val) => {
          this.getDeveloper(val, 1)
        }
      }
    }

    let formData = new FormData()
    formData.append('name', value)

    businessCommonMgr
      .getListDeveloperAbbreviation(formData)
      .then((res) => {
        developer = res
        if (!flag) {
          opt.tree.options = developer
        } else {
          this.all[3].tree.options = developer
        }
      })
    return opt
  }

  getChannels() {
    let opt = {
      title: '来源渠道',
      key: 'sourceChannels',
      placeholder: '全部',
      xl: 4,
      tree: {
        options: channels,
      }
    }
    let params = {}
    businessCommonMgr
      .getChannelList(params)
      .then((res) => {
        channels = res
        opt.tree.options = channels
      })

    return opt
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }


  createAll() {
    this.all = [
      this.getCompany(),
      {
        key: 'checkTime',
        xl: 4,
        dtTime: {},
        rules: [
          ruleOpt.timeInterval(2),
          // ruleOpt.check('不支持选择单边日期', (value) => {
          //   return value[0] == '' ^ value[1] == ''
          // })
        ]
      },
      this.getChannels(),
      this.getDeveloper('', 0),
      {
        title: '',
        placeholder: '请输入案场名称',
        xl: 3,
        key: 'buildingName',
        input: {}
      }
    ]
  }
}

export default SearchOpt